<template>
  <el-dialog v-model="openCreate" :title="title" width="30%" :before-close="handleClose">
    <el-form v-if="formMap.attachId==''" ref="uploadRef" :model="formMap" label-width="120px">
      <el-form-item label="图件模板">
        <el-select v-model="formMap.format" placeholder="请选择">
          <el-option label="A3横版" value="0" />
          <el-option label="A3竖版" value="1" />
          <el-option label="A4横版" value="2" />
          <el-option label="A4竖版" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="图件标题">
        <el-input v-model="formMap.title" style="width: 217px;" />
      </el-form-item>
      <el-form-item label="图件单位">
        <el-input v-model="formMap.unit" style="width: 217px;" />
      </el-form-item>
    </el-form>
    <el-form v-else :model="formMap" ref="uploadRef" :inline="true" class="chartForm"
              label-width="80px" v-loading="chartLoading">
      <el-form-item label="图件模板">
        <el-select v-model="formMap.format" placeholder="请选择">
          <el-option label="A3横版" value="0" />
          <el-option label="A3竖版" value="1" />
          <el-option label="A4横版" value="2" />
          <el-option label="A4竖版" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="图件标题">
        <el-input v-model="formMap.title" style="width: 217px;" />
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <el-form-item label="制图单位" prop="unit">
            <el-input v-model="formMap.unit" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="图片" prop="picture">
            <ImageUpload ref="reportImgUpload" :fileSize="100" :limit="1" downloadUrl="downLoadFile"
                          v-model="formMap.attach" :show-file-list="false" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="generateDrawings" v-if="formMap.attachId==''">生成图件</el-button>
        <el-button type="primary" @click="generateDrawings" v-else>重新生成图件</el-button>
        <el-button type="primary" @click="uploadImg" v-show="formMap.attachId!=''">下载图件</el-button>
        <el-button type="primary" @click="handleClose">关闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { supergraph,findFileById,getByParam } from '@/api/drawingView/index.js'
import ImageUpload from '@/map/components/drawingView/ImageUpload/index.vue'
import { ref } from 'vue';

const { proxy } = getCurrentInstance();
const props = defineProps(['open','title','formParams']);
const emits = defineEmits(['closeDialog'])

const formMap = ref({
  format: '',
  title: '',
  unit: '',
  attachId: '',
  attach: []
})
const chartLoading = ref(false)

function handleClose() {
  openCreate.value = false
  emits('closeDialog', true);
}
function generateDrawings() {
  proxy.$refs['uploadRef'].validate(valid => {
    if (valid) {
      proxy.$modal.loading("正在生成，请稍候...");
    //结构破坏：破坏程度 private String destory;
    //概率评估：T年 private String year;
      let params = {
        taskId: '',// 任务id
        dataType: '',// 0评估,1区划,2生命线
        qhType: '',// 0时单指标风险区划,1时综合指标风险区划
        assessType: '',// 评估方式:0为确定评估,1为概率评估
        assessObj: '',// 评估对象:0为结构破坏,1为人员伤亡,2为经济损失
        casualties: '',// 人员伤亡时: 1重伤,2死亡
        times: '',// 人员伤亡时:0白天,1夜晚
        // 地震输入方式:0为设定烈度,1为四个概率,2为地震影响场,3为发生概率危险性,4为超越概率危险性
        seismicInputMode: '',
        //确定评估：烈度时（ld1，ld2，ld3，ld4，ld5）；4个概率水准时（sgglsz1、sgglsz2、sgglsz3、sgglsz4）
        condition: '',
        destory: '',// 结构破坏：基本完好，轻微破坏，中等破坏，严重破坏，倒塌
        year: '',// 概率评估：T年
      }
      let form = props.formParams
      form.format = formMap.value.format
      form.title = formMap.value.title
      form.unit = formMap.value.unit
      form.attachId = formMap.value.attachId
      supergraph(form).then(res => {
        form.attachId = res.data
        getByParam(form).then(res => {
          if(res.data.length != 0) {
            formMap.value = res.data[0]
            let attachId = res.data[0].attach.attachId;
            let name = res.data[0].attach.attachName;
            let url = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId=" + encodeURIComponent(attachId) || '';
            formMap.value.attach = [{ attachId: attachId || '', name: name || '', url: url }];
          }else{
            formMap.value.attach = []
            formMap.value.attachId == ''
            formMap.value.format = ''
            formMap.value.title == ''
            formMap.value.unit = ''
          }
        }).catch(erro => {

        })
        proxy.$modal.closeLoading();
      }).catch(erro => {
        proxy.$modal.closeLoading();
      })
    }
  });
}
const openCreate = ref(false)
//获取查询参数
watch(() => props.open, value => {// watch函数监听props.open
  if(value){
    formMap.value.attachId = ''
    if(props.formParams.attachId != undefined){
      props.formParams.attachId = ''
      props.formParams.title = ''
      props.formParams.unit = ''
      props.formParams.format = ''
    }
    getByParam(props.formParams).then(res => {
      if(res.data.length != 0) {
        formMap.value = res.data[0]
        let attachId = res.data[0].attach.attachId;
        let name = res.data[0].attach.attachName;
        let url = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId=" + encodeURIComponent(attachId) || '';
        formMap.value.attach = [{ attachId: attachId || '', name: name || '', url: url }];
      }else{
        formMap.value.attach = []
        formMap.value.attachId = ''
        formMap.value.format = ''
        formMap.value.title = ''
        formMap.value.unit = ''
      }
      openCreate.value = true
    }).catch(erro => {

    })
  }
},{immediate:true});
//下载图件
function uploadImg() {
  if(formMap.value.attach.length>0){
    saveAs(formMap.value.attach[0].url, '图件')
  }
}

</script>